from http import HTTPStatus
from dashscope import Application


applications = {
    "code_assistant": "0ec158eca8b84a3e82c9ad92a46872ee",
    "dashScope_assistant": "a5ad5f8e4263415990fc1a64db7bd857",
    "svg_assistant": "550cacd04e4248428b2a94ef0ce8aec8"
}

def chat_with_ai(prompt, stream = False) -> str:
    responses = Application.call(
    api_key="sk-455cedfbda37476dbd92dde7e061a245",
    app_id=applications["dashScope_assistant"],
    prompt=prompt,
    stream=stream,
    incremental_output=True)

    for response in responses:
        if response.status_code != HTTPStatus.OK:
            print(f'request_id={response.request_id}')
            print(f'code={response.status_code}')
            print(f'message={response.message}')
            print(f'refer：https://help.aliyun.com/zh/model-studio/developer-reference/error-code')
        else:
            print(f'{response.output.text}\n')
            return ''
    return response.output.text